<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<title>List of Catalog</title>

<%@include file="/WEB-INF/pages/common/head.jsp" %>

<%
final String module = request.getParameter("module") ;
final String moduleClass = module.substring(0,1).toUpperCase() + module.substring(1) ;
final String moduleName = request.getParameter("moduleName") ;
%>

<!-- third party lib -->
<link rel="stylesheet" type="text/css" href="<%=root%>/plugin/flexigrid/css/flexigrid/flexigrid.css">
<script type="text/javascript" src="<%=root%>/plugin/flexigrid/flexigrid.js"></script>



<script type="text/javascript">
var dataGrid = null ;

window.onload=function (){
	var toolbar = [
                    {name :'添加兄弟目录',bclass:'add',onpress:toAddSibling},
                    {name :'添加子目录',bclass:'add',onpress:toAddChild},
	                {name :'修改',bclass:'edit',onpress:toEdit},
	                {name :'删除',bclass:'delete',onpress:doDelete},
	                {separator : true}
	               ] ;

    if (opener != null) {
    	toolbar[toolbar.length] = {name :'选取',bclass:'search',onpress:doSelect}
    }
    
    dataGrid = $("#dataGrid").flexigrid(
        {
            url: '<%=root%>/<%=module%>/catalog_list.action',
            dataType: 'xml',
            colModel : [
                {display: '<input type="checkbox" name="checkAll" onclick="checkIds(this)" />', name:"catalog_id", width:30, sortable: false, formater:idsCheckboxes},
                {display: 'Name', name : 'name_', width : 180, sortable : true, align: 'left'},
                {display: 'Pro_Id', name : 'pro_catalog_id', width : 100, sortable : true, align: 'center'},
                {display: 'Desc', name : 'desc_', width : 120, sortable : true, align: 'left'},
                {display: 'id_', name : 'catalog_id', width : 130, hide: true}
                ],
            buttons : toolbar,
            sortable: false,
            usepager: true,
            title: '<%=moduleName%>列表',
            useRp: true,
            rp: 15,
            showTableToggleBtn: false,
            width: 900,
            //onSubmit: doList,
            height: 420,
            searchDivName: "searchFormDiv",
            searchFormName:"searchForm",
            rowIdName:"catalog_id",
            rowProIdName:"pro_catalog_id",
            recordXPath:"root datas <%=moduleClass%>Catalog",
            treeColumn:1,
            isTreeView:true
        }
    );


    doList();

}

function doList() {
    dataGrid.flexReload() ;
    return true;
}

//var windowParams = getWindowParams(window.name) ;

function doSelect() {

    if(isDefined(opener.gridWindowOnSelected)) {
        opener.gridWindowOnSelected(dataGrid.getSelectedRows()[0])
    }

    window.close() ;
}

var windowMe = null ;
var pro_id = null ;
var pro_name = null ;
var level = null ;

function toAddSibling() {
    toAdd(false) ;
}

function toAddChild() {
    toAdd(true) ;
}

function toAdd(isAddChild) {
    var ids = dataGrid.getSelectedRows() ;
    
    if(ids.length==1){
        
        var record = xml2Object(ids[0]) ;

        if (isAddChild) {
            pro_id = record.catalog_id.innerText ;
            pro_name = record.name_.innerText ;
            level = (+record.level_.innerText) + 1 ;
        } else {
            pro_id = record.pro_catalog_id.innerText ;

            if (isUndefined(pro_id) || pro_id == "") {
            	//pro_id = "" ;
            	//pro_name = "(root)" ;
            	alert("根目录不能添加兄弟目录！") ;
            	return false ;
            } else {
                pro_name = xml2Object(dataGrid.getRowById(pro_id)).name_.innerText ;
            }
            level = (+record.level_.innerText) ;
        }
        
        windowMe = openWindow({url:"<%=root%>/<%=module%>/catalog_form.action", name:"addCatalog", width:400, height:400}) ;
        
    }else if(ids.length>1){
        alert("请选择一个修改,不能同时修改多个记录!");
    }else if(ids.length==0){
        alert("请选择一个您要修改的记录!")
    }
    
    //windowMe = openWindow({url:"<%=root%>/<%=module%>/catalog_form.action", name:"addCatalog", width:400, height:400}) ;
}

function toEdit() {
    var ids = dataGrid.getSelectedIds() ;
    
    if(ids.length==1){
        //alert(dataGrid.getSelectedRows()[0]);
        
        var pro_id = e_(ids[0]).pro_id ;

        if (isUndefined(pro_id) || pro_id == "") {
            pro_id = "" ;
            pro_name = "(root)" ;
        } else {
            pro_name = xml2Object(dataGrid.getRowById(pro_id)).name_.innerText ;
        }
        
        windowMe = openWindow({url:"<%=root%>/<%=module%>/catalog_form.action", name:"updateCatalog", width:400, height:400}) ;
        
    }else if(ids.length>1){
        alert("请选择一个修改,不能同时修改多个记录!");
    }else if(ids.length==0){
        alert("请选择一个您要修改的记录!")
    }
}

function doEditOnRetrun(xml) {
    doList();
    
    $('#dialogEdit').dialog("close");
}

function doDelete() {
    var ids = v_("ids") ;

    if (ids==null || ids.length <=0) {
        alert("请钩选要删除的目录！") ;
        return false;
    }
	
    if (dataGrid.hasChild(ids)) {
        alert("要删除的目录不能有子目录！") ;
        return false;
    }
    
    if (confirm("确定要删除数据吗？")) {
        $.post("<%=root%>/<%=module%>/catalog_delete.action", $(searchForm).serialize(), doDeleteOnRetrun); 
    }

}

function doDeleteOnRetrun(xml) {
    doList();
}

function idsCheckboxes(value) {
	if ((+value) == -1) {
        return "";
	}
    return "<input type='checkbox' value='" + value + "' name='ids' />" ;
}

function checkIds(checkboxAll) {
    var ids = document.all("ids") ;

    for (var i = 0 ; i < ids.length ; i ++) {
        ids[i].checked = checkboxAll.checked ;
    }
}

function formWindowOnClose(editForm) {
	doList() ;
}


window.onunload = function () {
	unblock();
}
</script>
</head>
<body topmargin="0" leftmargin="0">
  <form action="<%=root %>/<%=module%>/catalog_list.action" method="post" name="searchForm" id="searchForm"  topmargin="0" leftmargin="0" >
    <input type="hidden" name="pageSize"/>
    
    <table id="dataGrid" style="display:none">
    </table>
    <div style="display: none">
    <div id="searchFormDiv">
          <table width="100%" style="margin-top: 4px">
              <tr valign="middle">
                 <td>domain:<input type="text" name="catalog.domain_"/></td>
                 <td>code:<input type="text" name="catalog.pro_catalog_id"/></td>
                 <td>name:<input type="text" name="catalog.name_"/></td>
               </tr>
          </table>
    </div>
    </div>
  </form>
</body>
</html>